package com.inxedu.os.edu.controller.course;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.inxedu.os.edu.pojo.EduWebsiteCourse;
import com.inxedu.os.edu.pojo.QuerySubject;
import com.inxedu.os.edu.pojo.WebCourseRecommond;
import com.inxedu.os.edu.pojo.result;
import com.inxedu.os.edu.service.course.CourseService;
import com.inxedu.os.edu.service.course.RecommondCourse;
import com.inxedu.os.edu.util.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("admin")
@Api(value = "推荐课程", tags = "推荐课程")
public class RecommondController {
    @Autowired
    private RecommondCourse recommondCourse;

    @Autowired
    private CourseService courseService;
    /*
     * 推荐课程列表页
     *
     * */
    @RequestMapping(value = "/detail/list",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiImplicitParam(name = "eduWebsiteCourse", value = "推荐课程（课程表与推荐分类课程表）", required = true, dataType = "eduWebsiteCourse")
    @ApiOperation(value = "推荐课程列表页", notes = "推荐课程列表页")
    public String RecommonCourse(Model model,HttpServletRequest request, @RequestParam(defaultValue = "1")int page,EduWebsiteCourse eduWebsiteCourse){
        PageHelper.startPage(page,10);
        List<EduWebsiteCourse> eduWebsiteCourses = recommondCourse.selectRecommond(eduWebsiteCourse);
        PageInfo<List<EduWebsiteCourse>> info = new PageInfo(eduWebsiteCourses);
        model.addAttribute("dtoList",eduWebsiteCourses);
        model.addAttribute("page",info);
        request.getSession().setAttribute("detailListUri", WebUtils.getServletRequestUriParms(request));
        //查询推荐分类
        List<EduWebsiteCourse> recommond=recommondCourse.findrecommondCourse();
        request.setAttribute("websiteCourseList",recommond);
        return "view/inxedu/admin/website/course/websiteCourseDetail_list" ;
    }
    //删除
    @RequestMapping(value = "/detail/deletedeail/{id}",method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "课程Id", required = true, dataType = "id")
    @ApiOperation(value = "推荐课程删除", notes = "推荐课程删除")
    public ModelAndView deleteCourese(@PathVariable("id")Long id, HttpServletRequest request){
        int course = recommondCourse.deleteCourse(id);
        ModelAndView model =new ModelAndView();
        Object uri = request.getSession().getAttribute("detailListUri");
        if(uri!=null){
            model.setViewName("redirect:"+uri.toString());
        }else{
            model.setViewName("redirect:view/inexdu/admin.detail/list");
        }


        return model;

    }
    //修改排序
    @RequestMapping(value = "/detail/updatesort/{id}/{sort}",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "课程Id", required = true, dataType = "id"),
            @ApiImplicitParam(name = "ordelNum", value = "课程显示排序", required = true, dataType = "ordelNum")
    })

    @ApiOperation(value = "推荐课程删除", notes = "推荐课程删除")
    @ResponseBody
    public result updateSort(@PathVariable("id") Long id, @PathVariable("sort")Long ordelNum, HttpServletRequest request){

        int i = recommondCourse.updateSort(id,ordelNum);

        result result =new result();
        if(i>0){
            result.setMessage("SUCCESS");
            result.setSuccess(true);

        }else {
            result.setMessage("FALSE");
            result.setSuccess(false);
        }
        /*  Object uri = request.getSession().getAttribute("detailListUri");*/
        /*   ModelAndView model =new ModelAndView();*/

    /*    if(uri!=null){
            model.setViewName("redirect:"+uri.toString());
        }else{
            model.setViewName("redirect:view/inexdu/admin.detail/list");
        }*/
        return result;

    }

    //显示推荐课程窗口
    @RequestMapping(value = "/cou/showrecommendList",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiImplicitParam(name = "webCourseRecommond", value = "推荐课程对象", required = true, dataType = "webCourseRecommond")
    @ApiOperation(value = "显示新增推荐课程窗口", notes = "显示新增推荐课程窗口")
    public String toCourselist(Model model,@RequestParam(defaultValue = "1") int page,WebCourseRecommond webCourseRecommond){

        Gson gson=new Gson();
        //查询专业
        PageHelper.startPage(page,10);
        List<WebCourseRecommond> webCourseRecommonds=recommondCourse.findWebCourseRecommond(webCourseRecommond);
        PageInfo<WebCourseRecommond> info = new PageInfo(webCourseRecommonds);

        //查询推荐分类
        List<EduWebsiteCourse> recommond=recommondCourse.findrecommondCourse();

        model.addAttribute("page",info);
        model.addAttribute("courseList",webCourseRecommonds);
        model.addAttribute("webstieList",recommond);


        return "view/inxedu/admin/course/course_recommend_list";
    }

    public Map<String, Object> setJson(boolean success, String message, Object entity) {
        Map<String, Object> json = new HashMap();
        json.put("success", success);
        json.put("message", message);
        json.put("entity", entity);
        return json;
    }
    //添加推荐课程
    @RequestMapping(value = "/detail/addrecommendCourse",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseIds", value = "课程Id", required = true, dataType = "courseIds"),
            @ApiImplicitParam(name = "recommendId", value = "推荐Id", required = true, dataType = "recommendId")
    })

    @ApiOperation(value = "添加推荐课程", notes = "添加推荐课程")
    @ResponseBody
    public Map<String, Object> addrecommendCourse(@RequestParam String courseIds,@RequestParam  Integer recommendId ,HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();

        int i=recommondCourse.addrecommendCourse(courseIds,recommendId);
        Gson gson=new Gson();
        if(i>0) {
            map = this.setJson(true, "添加成功！", null);
        }else {
            map = this.setJson(false, "添加失败！！", null);
        }
        return map;
    }





}
